// index.js
// 获取应用实例
const app = getApp()

Page({
    data: {
        imgList: [
            {
                url: app.globalData.url + "/image/ipadpro01.png",
                title: "ipad Pro"
            },
            {
                url: app.globalData.url + "/image/ipadmini.png",
                title: "ipad Mini"
            },
            {
                url: app.globalData.url + "/image/applewatch.jpg",
                title: "Apple Watch"
            },
            {
                url: app.globalData.url + "/image/macbookpro.jpg",
                title: "MacBook Pro"
            }
        ],
        items: [],
        index: 0,
        url: app.globalData.url,
    },
    initData() {
        if (this.data.index) {
            clearInterval(this.data.index)
        }
        let items = this.data.items
        items.forEach((e) => {
            // 秒杀开始倒计时
            e.startTime = (new Date(e.killTime.replace(/-/g, "/")).getTime() - new Date().getTime()) / 1000
            // 秒杀倒计时
            e.times = (new Date(e.killTime.replace(/-/g, "/")).getTime() + 30 * 60 * 1000 - new Date().getTime()) / 1000;
            e.sales = 0
        })
        items.forEach(e => {
            console.log(e.times);
            e.start = this.timeFormatter(e.startTime, 1)
            e.time = e.startTime > 0 ? '秒杀还未开始' : this.timeFormatter(e.times)
        })
        this.setData({ items })
        let index = setInterval(() => {
            this.countdown()
        }, 1000)
        this.setData({ index })
    },
    // 格式化倒计时时间
    timeFormatter(time, start) {
        if (time < 0) {
            return start == 1 ? "秒杀开始" : "秒杀结束";
        } else {
            let second = Math.floor(time % 60)
            let min = Math.floor(time / 60);  // 分
            let hour = Math.floor(min / 60) // 小时
            min = min % 60
            let day = Math.floor(hour / 24) // 天
            hour = hour % 60
            let info = "";
            if (day != 0) {
                info = info + (day >= 10 ? day : "0" + day) + "天"
            }
            info = info + (hour >= 10 ? hour : "0" + hour) + ":"
            info = info + (min >= 10 ? min : "0" + min) + ":"
            info = info + (second >= 10 ? second : "0" + second)
            return info
        }
    },
    countdown() {
        let items = this.data.items
        items.forEach((e) => {
            e.startTime = e.startTime - 1
            e.times = e.startTime > 0 ? e.times : e.times - 1
        })
        items.forEach(e => {
            e.start = this.timeFormatter(e.startTime, 1)
            e.time = e.startTime > 0 ? '秒杀还未开始' : this.timeFormatter(e.times)
        })
        this.setData({ items })
    },
    onShow() {
        if (this.data.items) {
            this.initData()
        }
    },
    onLoad() {
        const _this = this
        wx.request({
            url: _this.data.url + '/kill/list',
            method: "GET",
            success(res) {
                if (res.data.code == 1) { // 请求成功
                    console.log(res.data.result);
                    _this.setData({
                        items: res.data.result
                    })
                }
                _this.initData()
            }
        })
    },
})
